#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tempfile
import os.path
from getpass import getpass
from xml.etree.ElementTree import *

import gdata.photos.service
import gdata.media
import gdata.geo

import piwadb
import unittest

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

import locale
console_encode = locale.getpreferredencoding()


class TestSequenceFunctions(unittest.TestCase) :

    def setUp(self) :
        self.map = None

    def tearDown(self) :
        self.db.close()

    def testadd(self) :
        self.db = piwadb.PiwaDb(':memory:')
        self.db.addAlbum(u'album_id_1', u'test album', u'public', u'0 0', u'http://www.google.com', u'http://www.google.com')
        self.db.addPhoto(u'album_id_1', u'photo_id_1', u'photo title', u'0 0', u'http://www.google.com', u'http://www.google.com')
        self.db.addTag(u'photo_id_1', u'tag1')
        self.db.addTag(u'photo_id_1', u'tag2')
        self.db.addTag(u'photo_id_1', u'tag3')
        self.map = self.db.tagmap()
        self.assertEqual(self.map['tag1'], 0 )
        self.assertEqual(self.map['tag2'], 1 )
        self.assertEqual(self.map['tag3'], 2 )
        self.assertEqual(len(self.map), 3)

    def testaddAndUpdate(self) :
        self.db = piwadb.PiwaDb(':memory:')
        self.db.addAlbum(u'album_id_1', u'test album', u'public', u'0 0', u'http://www.google.com', u'http://www.google.com')
        self.db.addPhoto(u'album_id_1', u'photo_id_1', u'photo title', u'0 0', u'http://www.google.com', u'http://www.google.com')
        self.db.addTag(u'photo_id_1', u'tag1')
        self.db.addTag(u'photo_id_1', u'tag2')
        self.db.addTag(u'photo_id_1', u'tag3')
        for album_row in self.db.albumList() :
            self.assertEqual(album_row['album_id'], u'album_id_1')
            self.assertEqual(album_row['album_title'], u'test album')
            self.assertEqual(album_row['album_access'], u'public')
            self.assertEqual(album_row['album_pos'], u'0 0')
            self.assertEqual(album_row['album_link'], u'http://www.google.com')
            self.assertEqual(album_row['album_thumbnail'], u'http://www.google.com')

        self.db.addAlbum(u'album_id_1', u'test album update', u'public', u'0 0', u'http://www.google.com', u'http://www.google.com')
        self.db.addPhoto(u'album_id_1', u'photo_id_1', u'photo title update', u'0 0', u'http://www.google.com', u'http://www.google.com')
        self.db.addTag(u'photo_id_1', u'tag1')
        self.db.addTag(u'photo_id_1', u'tag2')
        self.db.addTag(u'photo_id_1', u'tag3')

        for album_row in self.db.albumList() :
            self.assertEqual(album_row['album_id'], u'album_id_1')
            self.assertEqual(album_row['album_title'], u'test album update')
            self.assertEqual(album_row['album_access'], u'public')
            self.assertEqual(album_row['album_pos'], u'0 0')
            self.assertEqual(album_row['album_link'], u'http://www.google.com')
            self.assertEqual(album_row['album_thumbnail'], u'http://www.google.com')

    def testfromWeb(self) :
        
        dbfilename = tempfile.mkstemp(suffix=u'.splite', prefix=u'test_piwadb')

        print dbfilename

        self.db = piwadb.PiwaDb()
        gd_client = gdata.photos.service.PhotosService()
        gd_client.source = 'sindobook'
        gd_client.email = 'sindobook@gmail.com'
        gd_client.password = getpass("login password:")

        gd_client.ProgrammaticLogin()

        self.db.fromWeb(gd_client, owner='sindobook', console_encode=console_encode, album_only=False)

   

if __name__ == "__main__" :
    unittest.main()
